﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class SettingMenu : Menu
    {
        // Event for select main menu
        public delegate void EventHandleMenuSelect(int choice);
        public event EventHandleMenuSelect MenuSelect;

        private SettingMenuWithXML _settingMenuWithXMl;
        private GameWindow _window;
        private List<MyButton> _listMenuOption;

        public List<MyButton> ListMenuOption
        {
            get { return _listMenuOption; }
            set { _listMenuOption = value; }
        }
        private GameSettingSetup _gameSettingSetup;

        public GameSettingSetup GameSettingSetup
        {
            get { return _gameSettingSetup; }
            set { _gameSettingSetup = value; }
        }
        private List<MyButton> listLeft;
        private List<MyButton> listRight;
        private List<Sprite2D> listResolution;
        private List<Sprite2D> listFullScreen;
        private List<Sprite2D> listMouseSpeed;
        private List<Sprite2D> listVoiceVolume;
        private List<Sprite2D> listMusicVolume;

        private Sprite2D _backGround;

        public Sprite2D BackGround
        {
            get { return _backGround; }
            set { _backGround = value; }
        }

        private Sprite2D _border;

        public Sprite2D Border
        {
            get { return _border; }
            set { _border = value; }
        }

        private Sprite2D screenResolution, fullScreen, mouseSpeed, voiceVolume, musicVolume;

        public SettingMenu(GameWindow window, ContentManager content, GraphicsDevice graphicsDevice, string strmenuxml)
        {
            _window = window;
            _settingMenuWithXMl = content.Load<SettingMenuWithXML>(strmenuxml);
            _gameSettingSetup = new GameSettingSetup();
            SizeScale = _settingMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            InitMenu(content);
        }

        public void InitMenu(ContentManager content)
        {
            BackGround = LoadStaticItem(content, _settingMenuWithXMl.strBackGround, _settingMenuWithXMl.sizeScale, new Vector2(0, 0),"",new Vector2(0,0));
            screenResolution = LoadStaticItem(content, _settingMenuWithXMl.strResolution, _settingMenuWithXMl.sizeScaleResolution, _settingMenuWithXMl.positionScaleResolution, "", new Vector2(0, 0));
            fullScreen = LoadStaticItem(content, _settingMenuWithXMl.strFullScreen, _settingMenuWithXMl.sizeScaleFullScreen, _settingMenuWithXMl.positionScaleFullScreen, "", new Vector2(0, 0));
            mouseSpeed = LoadStaticItem(content, _settingMenuWithXMl.strMouseSpeed, _settingMenuWithXMl.sizeScaleMouseSpeed, _settingMenuWithXMl.positionScaleMouseSpeed, "", new Vector2(0, 0));
            musicVolume = LoadStaticItem(content, _settingMenuWithXMl.strMusicVolume, _settingMenuWithXMl.sizeScaleMusicVolume, _settingMenuWithXMl.positionScaleMusicVolume, "", new Vector2(0, 0));
            voiceVolume = LoadStaticItem(content, _settingMenuWithXMl.strVoiceVolume, _settingMenuWithXMl.sizeScaleVoiceVolume, _settingMenuWithXMl.positionScaleVoiceVolume, "", new Vector2(0, 0));
            Border = LoadStaticItem(content, _settingMenuWithXMl.strBorder, _settingMenuWithXMl.sizeScaleBorder, _settingMenuWithXMl.positionScaleBorder, "", new Vector2(0, 0));

            ListMenuOption = new List<MyButton>();
            ListMenuOption = LoadListItem(content, _settingMenuWithXMl.spriteMenuXML, "SettingMenu/MenuOption/", new Vector2(0, 0));
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }

            listLeft = new List<MyButton>();
            listLeft = LoadListItem(content, _settingMenuWithXMl.listLeft, "SettingMenu/MenuOption/", new Vector2(0, 0));
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }


            listRight = new List<MyButton>();
            listRight = LoadListItem(content, _settingMenuWithXMl.listRight, "SettingMenu/MenuOption/", new Vector2(0, 0));
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }

            listResolution = new List<Sprite2D>();
            listResolution = LoadStaticListItem(content, _settingMenuWithXMl.listResolution,"SettingMenu/");

            listFullScreen = new List<Sprite2D>();
            listFullScreen = LoadStaticListItem(content, _settingMenuWithXMl.listFullScreen,"SettingMenu/");

            listMouseSpeed = new List<Sprite2D>();
            listMouseSpeed = LoadStaticListItem(content, _settingMenuWithXMl.listMouseSpeed,"SettingMenu/");

            listVoiceVolume = new List<Sprite2D>();
            listVoiceVolume = LoadStaticListItem(content, _settingMenuWithXMl.listVoiceVolume, "SettingMenu/");

            listMusicVolume = new List<Sprite2D>();
            listMusicVolume = LoadStaticListItem(content, _settingMenuWithXMl.listMusicVolume, "SettingMenu/");
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            BackGround.Size = Size;
            Border.Size = new Vector2(_settingMenuWithXMl.sizeScaleBorder.X * Size.X, _settingMenuWithXMl.sizeScaleBorder.Y * Size.Y);
            Border.Position = new Vector2(_settingMenuWithXMl.positionScaleBorder.X * Size.X, _settingMenuWithXMl.positionScaleBorder.Y * Size.Y);

            screenResolution.Size = new Vector2(_settingMenuWithXMl.sizeScaleResolution.X * Size.X, _settingMenuWithXMl.sizeScaleResolution.Y * Size.Y);
            screenResolution.Position = new Vector2(_settingMenuWithXMl.positionScaleResolution.X * Size.X, _settingMenuWithXMl.positionScaleResolution.Y * Size.Y);

            fullScreen.Size = new Vector2(_settingMenuWithXMl.sizeScaleFullScreen.X * Size.X, _settingMenuWithXMl.sizeScaleFullScreen.Y * Size.Y);
            fullScreen.Position = new Vector2(_settingMenuWithXMl.positionScaleFullScreen.X * Size.X, _settingMenuWithXMl.positionScaleFullScreen.Y * Size.Y);

            mouseSpeed.Size = new Vector2(_settingMenuWithXMl.sizeScaleMouseSpeed.X * Size.X, _settingMenuWithXMl.sizeScaleMouseSpeed.Y * Size.Y);
            mouseSpeed.Position = new Vector2(_settingMenuWithXMl.positionScaleMouseSpeed.X * Size.X, _settingMenuWithXMl.positionScaleMouseSpeed.Y * Size.Y);

            musicVolume.Size = new Vector2(_settingMenuWithXMl.sizeScaleMusicVolume.X * Size.X, _settingMenuWithXMl.sizeScaleMusicVolume.Y * Size.Y);
            musicVolume.Position = new Vector2(_settingMenuWithXMl.positionScaleMusicVolume.X * Size.X, _settingMenuWithXMl.positionScaleMusicVolume.Y * Size.Y);

            voiceVolume.Size = new Vector2(_settingMenuWithXMl.sizeScaleVoiceVolume.X * Size.X, _settingMenuWithXMl.sizeScaleVoiceVolume.Y * Size.Y);
            voiceVolume.Position = new Vector2(_settingMenuWithXMl.positionScaleVoiceVolume.X * Size.X, _settingMenuWithXMl.positionScaleVoiceVolume.Y * Size.Y);

            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].SetPosition(new Vector2(_settingMenuWithXMl.spriteMenuXML[i].positionScale.X * Size.X, _settingMenuWithXMl.spriteMenuXML[i].positionScale.Y * Size.Y));
                ListMenuOption[i].SetSize(new Vector2(_settingMenuWithXMl.spriteMenuXML[i].sizeScale.X * Size.X, _settingMenuWithXMl.spriteMenuXML[i].sizeScale.Y * Size.Y));
            }
            for (int i = 0; i < listLeft.Count; i++)
            { 
                listLeft[i].SetPosition(new Vector2(_settingMenuWithXMl.listLeft[i].positionScale.X * Size.X, _settingMenuWithXMl.listLeft[i].positionScale.Y * Size.Y));
                listLeft[i].SetSize(new Vector2(_settingMenuWithXMl.listLeft[i].sizeScale.X * Size.X, _settingMenuWithXMl.listLeft[i].sizeScale.Y * Size.Y));
            }
            for (int i = 0; i < listRight.Count; i++)
            { 
                listRight[i].SetPosition(new Vector2(_settingMenuWithXMl.listRight[i].positionScale.X * Size.X, _settingMenuWithXMl.listRight[i].positionScale.Y * Size.Y));
                listRight[i].SetSize(new Vector2(_settingMenuWithXMl.listRight[i].sizeScale.X * Size.X, _settingMenuWithXMl.listRight[i].sizeScale.Y * Size.Y));
            }
            for (int i = 0; i < listResolution.Count; i++)
            {
                listResolution[i].Position = new Vector2(_settingMenuWithXMl.listResolution[i].positionScale.X * Size.X, _settingMenuWithXMl.listResolution[i].positionScale.Y * Size.Y);
                listResolution[i].Size = new Vector2(_settingMenuWithXMl.listResolution[i].sizeScale.X * Size.X, _settingMenuWithXMl.listResolution[i].sizeScale.Y * Size.Y);
            }
            for (int i = 0; i < listFullScreen.Count; i++)
            {
                listFullScreen[i].Position = new Vector2(_settingMenuWithXMl.listFullScreen[i].positionScale.X * Size.X, _settingMenuWithXMl.listFullScreen[i].positionScale.Y * Size.Y);
                listFullScreen[i].Size = new Vector2(_settingMenuWithXMl.listFullScreen[i].sizeScale.X * Size.X, _settingMenuWithXMl.listFullScreen[i].sizeScale.Y * Size.Y);
            }
            for (int i = 0; i < listMouseSpeed.Count; i++)
            {
                listMouseSpeed[i].Position = new Vector2(_settingMenuWithXMl.listMouseSpeed[i].positionScale.X * Size.X, _settingMenuWithXMl.listMouseSpeed[i].positionScale.Y * Size.Y);
                listMouseSpeed[i].Size = new Vector2(_settingMenuWithXMl.listMouseSpeed[i].sizeScale.X * Size.X, _settingMenuWithXMl.listMouseSpeed[i].sizeScale.Y * Size.Y);
            }
            for (int i = 0; i < listMusicVolume.Count; i++)
            {
                listMusicVolume[i].Position = new Vector2(_settingMenuWithXMl.listMusicVolume[i].positionScale.X * Size.X, _settingMenuWithXMl.listMusicVolume[i].positionScale.Y * Size.Y);
                listMusicVolume[i].Size = new Vector2(_settingMenuWithXMl.listMusicVolume[i].sizeScale.X * Size.X, _settingMenuWithXMl.listMusicVolume[i].sizeScale.Y * Size.Y);
            }
            for (int i = 0; i < listVoiceVolume.Count; i++)
            {
                listVoiceVolume[i].Position = new Vector2(_settingMenuWithXMl.listVoiceVolume[i].positionScale.X * Size.X, _settingMenuWithXMl.listVoiceVolume[i].positionScale.Y * Size.Y);
                listVoiceVolume[i].Size = new Vector2(_settingMenuWithXMl.listVoiceVolume[i].sizeScale.X * Size.X, _settingMenuWithXMl.listVoiceVolume[i].sizeScale.Y * Size.Y);
            }
        }

        //Process when option was clicked
        public void ClickButtonWithSender(object sender)
        {
            int choice = ListMenuOption.IndexOf((MyButton)sender);
            if (choice != -1) MenuSelect(choice);
            else 
            {
                choice = listLeft.IndexOf((MyButton)sender);
                if (choice != -1)
                {
                    switch (choice)
                    { 
                        case 0:
                            GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateScreenResolution, GameSettingSetup.Navigate.Back);
                            break;
                        case 1:
                            GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateFullScreen, GameSettingSetup.Navigate.Back);
                            break;
                        case 2:
                            GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateMouseSpeed, GameSettingSetup.Navigate.Back);
                            break;
                        case 3:
                            GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateMusicVolume, GameSettingSetup.Navigate.Back);
                            break;
                        case 4:
                            GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateVoiceVolume, GameSettingSetup.Navigate.Back);
                            break;
                    }
                }
                else
                {
                    choice = listRight.IndexOf((MyButton)sender);
                    if (choice != -1)
                    {
                        switch (choice)
                        {
                            case 0:
                                GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateScreenResolution, GameSettingSetup.Navigate.Next);
                                break;
                            case 1:
                                GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateFullScreen, GameSettingSetup.Navigate.Next);
                                break;
                            case 2:
                                GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateMouseSpeed, GameSettingSetup.Navigate.Next);
                                break;
                            case 3:
                                GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateMusicVolume, GameSettingSetup.Navigate.Next);
                                break;
                            case 4:
                                GameSettingSetup.Update(GameSettingSetup.TypeUpdate.UpdateVoiceVolume, GameSettingSetup.Navigate.Next);
                                break;
                        }
                    }
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            BackGround.Update(gameTime);
            Border.Update(gameTime);
            screenResolution.Update(gameTime);
            fullScreen.Update(gameTime);
            mouseSpeed.Update(gameTime);
            musicVolume.Update(gameTime);
            voiceVolume.Update(gameTime);
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].Update(gameTime);
            }
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].Update(gameTime);
            }
            switch (GameSettingSetup.ScreenResolution)
            { 
                case ScreenResolution.sr1024x600:
                    listResolution[1].Update(gameTime);
                    break;
                case ScreenResolution.sr1366x768:
                    listResolution[0].Update(gameTime);
                    break;
                case ScreenResolution.sr800x600:
                    listResolution[2].Update(gameTime);
                    break;
            }
            switch (GameSettingSetup.IsFullScreen)
            { 
                case true:
                    listFullScreen[0].Update(gameTime);
                    break;
                case false:
                    listFullScreen[1].Update(gameTime);
                    break;
            }
            switch (GameSettingSetup.MouseSpeed)
            { 
                case MouseSpeed.Low:
                    listMouseSpeed[0].Update(gameTime);
                    break;
                case MouseSpeed.Medium:
                    listMouseSpeed[1].Update(gameTime);
                    break;
                case MouseSpeed.High:
                    listMouseSpeed[2].Update(gameTime);
                    break;
            }
            switch (GameSettingSetup.MusicVolume)
            {
                case MusicVolume.Low:
                    listMusicVolume[0].Update(gameTime);
                    break;
                case MusicVolume.Medium:
                    listMusicVolume[1].Update(gameTime);
                    break;
                case MusicVolume.High:
                    listMusicVolume[2].Update(gameTime);
                    break;
            }
            switch (GameSettingSetup.VoiceVolume)
            {
                case VoiceVolume.Low:
                    listVoiceVolume[0].Update(gameTime);
                    break;
                case VoiceVolume.Medium:
                    listVoiceVolume[1].Update(gameTime);
                    break;
                case VoiceVolume.High:
                    listVoiceVolume[2].Update(gameTime);
                    break;
            }
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            BackGround.Draw(gameTime, spriteBatch);
            Border.Draw(gameTime,spriteBatch);
            screenResolution.Draw(gameTime, spriteBatch);
            fullScreen.Draw(gameTime, spriteBatch);
            mouseSpeed.Draw(gameTime, spriteBatch);
            musicVolume.Draw(gameTime, spriteBatch);
            voiceVolume.Draw(gameTime, spriteBatch);
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].Draw(gameTime, spriteBatch);
            }
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].Draw(gameTime, spriteBatch);
            }
            switch (GameSettingSetup.ScreenResolution)
            {
                case ScreenResolution.sr1024x600:
                    listResolution[1].Draw(gameTime, spriteBatch);
                    break;
                case ScreenResolution.sr1366x768:
                    listResolution[0].Draw(gameTime, spriteBatch);
                    break;
                case ScreenResolution.sr800x600:
                    listResolution[2].Draw(gameTime, spriteBatch);
                    break;
            }
            switch (GameSettingSetup.IsFullScreen)
            {
                case true:
                    listFullScreen[0].Draw(gameTime, spriteBatch);
                    break;
                case false:
                    listFullScreen[1].Draw(gameTime, spriteBatch);
                    break;
            }
            switch (GameSettingSetup.MouseSpeed)
            {
                case MouseSpeed.Low:
                    listMouseSpeed[0].Draw(gameTime, spriteBatch);
                    break;
                case MouseSpeed.Medium:
                    listMouseSpeed[1].Draw(gameTime, spriteBatch);
                    break;
                case MouseSpeed.High:
                    listMouseSpeed[2].Draw(gameTime, spriteBatch);
                    break;
            }
            switch (GameSettingSetup.MusicVolume)
            {
                case MusicVolume.Low:
                    listMusicVolume[0].Draw(gameTime, spriteBatch);
                    break;
                case MusicVolume.Medium:
                    listMusicVolume[1].Draw(gameTime, spriteBatch);
                    break;
                case MusicVolume.High:
                    listMusicVolume[2].Draw(gameTime, spriteBatch);
                    break;
            }
            switch (GameSettingSetup.VoiceVolume)
            {
                case VoiceVolume.Low:
                    listVoiceVolume[0].Draw(gameTime, spriteBatch);
                    break;
                case VoiceVolume.Medium:
                    listVoiceVolume[1].Draw(gameTime, spriteBatch);
                    break;
                case VoiceVolume.High:
                    listVoiceVolume[2].Draw(gameTime, spriteBatch);
                    break;
            }
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].Draw(gameTime, spriteBatch);
            }
        }
    }
}
